// =============================================================
// CInterpreter
// ---------------------------------------
// Author : Woo-Hyun Kim
// Email : woorung@icu.ac.kr or woorung@nhncorp.com
// Date : Aug. 8, 2006
//----------------------------------------------------------------------
#include "CInterpreter.h"
//----------------------------------------------------------------------
CInterpreter::CInterpreter(string &name) : CProcess()
{
	if(name[0]=='@') this->call_name = name.substr(1);
	else this->call_name = name;
}
//----------------------------------------------------------------------
CInterpreter::~CInterpreter()
{
}
//----------------------------------------------------------------------
int CInterpreter::read_upstream(const void *buf, size_t size) 
{
	cout << (char *)buf;
}
//----------------------------------------------------------------------
int CInterpreter::read_downstream(const void *buf, size_t size) 
{
}
//----------------------------------------------------------------------
void CInterpreter::before_run() 
{
}
//----------------------------------------------------------------------
void CInterpreter::invoke(map<string,string> &common_task_variables, map<string,string> &task_variables, map<string,string> &action_variables)
{
	// add common task variables to params
	map<string,string>::iterator variable = common_task_variables.begin();
	while(variable!=common_task_variables.end()) {
		string attr;
		if(variable->first[0]=='@') attr = variable->first.substr(1);
		else attr = variable->first;

		string val = variable->second;

		params[attr] = val;

		variable++;
	}

	// add task variables to params
	variable = task_variables.begin();
	while(variable!=task_variables.end()) {
		string attr;
		if(variable->first[0]=='@') attr = variable->first.substr(1);
		else attr = variable->first;

		string val = variable->second;

		params[attr] = val;

		variable++;
	}

	invoke(action_variables);
	//method_returns = invoke(action_variables, method_returns);

	//return method_returns;
}
//----------------------------------------------------------------------
void CInterpreter::invoke(map<string,string> &action_variable)
{
	map<string,string>::iterator param = action_variable.begin();
	while(param!=action_variable.end()) {
        	string attr;
        	if(param->first[0]=='@') attr = param->first.substr(1);
        	else attr = param->first;

        	string val = param->second;

        	this->params[attr] = val;

        	param++;
    	}

	// run a process
	this->start();

	// wait for its exit
	if(this->waits()<0) {
		cout << "////////////////////////////////////////////////////////" << endl;
		cout << this->get_pid() << " was abnormally exited" << endl;
		cout << "////////////////////////////////////////////////////////" << endl;
		exit(-1);
	}

	//------------------------------------------
	// save the results from the internal pipe
	//------------------------------------------
	//map<string,string>::iterator result = returns.begin();
	//while(result!=returns.end()) {
	//	method_returns[result->first] = result->second;
//
//		result++;
//	}
	//------------------------------------------

	//return method_returns;
}
//----------------------------------------------------------------------
void CInterpreter::run() {
	// must override at subclasses
}
//----------------------------------------------------------------------
